##putting together a monte carlo simulation making use of continuous covariates with sparse effects


monte.func.ucs<- function(N = 2500, folds = 10, files, speed){




source('/nfs/home/J/jgrimmer/het/SLF_round2.R')

len<- length


##the first is very simply a model that finds a slight variation in a discrete covariates
weight.mat<- matrix(NA, nrow=50, ncol=7)
mse.mat<- matrix(NA, nrow=50, ncol=9)
cor.mat<- array(NA, dim = c(9,9,50))

monte <- 1

N<- N
folds<- folds


treats<- t(rmultinom(N, prob = rep(1/47, 47), size = 1))[,1:46]


inter<- 0
beta<- c(2, 1, 0.5, -1, -2, runif(41, -0.07, 0.07) )
x1<- rnorm(N)
x2<- rnorm(N)
x3<- rnorm(N)

x.in<- sin(x1)*x2 + cos(x3)*x1
x.in2<- exp(x1/10)*x2^2 + exp(x1/10)*sin(x3)

covs<- cbind(x.in, x.in2)


gamma<- c(0.5, -0.5)
interact_t1<- c(-0.5, -0.75)
interact_t2<- c(0.5, -0.75)
interact_t3<- c(1, 0)
interact_t4<- c(0.5, 0.5)
interact_t5<- c(0.5, 0.75)
interact<- rbind(interact_t1, interact_t2, interact_t3, interact_t4, interact_t5, matrix(NA, nrow=41, ncol=2))
for(z in 6:46){
	interact[z,]<- runif(2, min= -0.1, max = 0.1)}




model.mat<- model.matrix(~covs*treats)
coefs<- c(inter, gamma, beta)
for(z in 1:46){
	coefs<- c(coefs, interact[z,])
	}
	


eff<- model.mat%*%coefs

ys<- c()
for(z in 1:N){
	
	ys[z]<- rbinom(1, prob = pnorm(eff[z]), size = 1)
	}


mm2<- model.mat
	

part1<- supLearnFit(covs, treats, ys, nfold=folds, speed=speed)

part2<- wrap.func(covs, ys, treats, covs, ys, treats, speed=speed)

##so now, creating the possible strata

##creating a sequence that is about 10 long on each
##for 100 total combinations

seq1<- seq(min(x.in), max(x.in), len = 10)
seq2<- seq(min(x.in2), max(x.in2), len = 10)


pot.covs<- matrix(NA, nrow=100, ncol=2)
a<- 0 
for(bb in 1:10){
	for(cc in 1:10){
		a<- a + 1
		pot.covs[a, ]<- c(seq1[bb], seq2[cc])
		}
		}



pos.treats<- matrix(0, nrow=46, ncol=46)
diag(pos.treats)<- 1

final.out<- matrix(NA, nrow=4600, ncol=141)
final.out[,1]<- 1
a<- 0



seq1<- seq(50, 141, by=2)
seq2<- seq(51, 141, by=2)
for(z in 1:nrow(pos.treats)){
	for(j in 1:nrow(pot.covs)){
	a<- a + 1
	final.out[a,2:3]<- pot.covs[j,] 
	final.out[a,4:49]<- pos.treats[z,]
	for(m in 1:ncol(treats)){
	final.out[a,seq1[m]:seq2[m]]<- pos.treats[z,m]*pot.covs[j,]}
	}
	}


new.treats<- final.out#[1:20,]	
colnames(new.treats)<- colnames(mm2)
colnames(new.treats)<- gsub('X', '', colnames(new.treats))
#colnames(new.treats)<- gsub('covsx.in2', 'X', colnames(new.treats))
#colnames(new.treats) <- gsub('x.in', '', colnames(new.treats))
#colnames(new.treats) <- gsub('x.in2', '', colnames(new.treats))
colnames(new.treats)<- gsub('treats', 'treat', colnames(new.treats))

new.conts<- new.treats
new.conts[,c(4:141)]<- 0



                             
colnames(new.conts)<- colnames(mm2)
colnames(new.conts)<- gsub('X', '', colnames(new.conts))
#colnames(new.conts)<- gsub('covsx.in', 'X', colnames(new.conts))
#colnames(new.conts)<- gsub('covsx.in2', 'X', colnames(new.conts))
#colnames(new.conts) <- gsub('x.in', '', colnames(new.conts))
#colnames(new.conts) <- gsub('x.in2', '', colnames(new.conts2))
colnames(new.conts)<- gsub('treats', 'treat', colnames(new.conts))


preds<- predict.weight(part1, part2, new.treats, new.conts, model.mat, ys)


treat.eff<- preds$Treat - preds$Control
true.eff<- pnorm(new.treats%*%coefs) - pnorm(new.conts%*%coefs)


final.est<- c()
for(z in 1:nrow(preds$Treat)){
	final.est[z]<- sum((preds$Treat[z, ] - preds$Control[z,])*preds$Weight)}

mses<- c()
for(z in 1:ncol(treat.eff)){
	mses[z]<- sqrt( sum( (treat.eff[,z] - true.eff)^2)/nrow(treat.eff))
	}
mses[ncol(treat.eff) + 1]<- sqrt(sum( (final.est - true.eff)^2)/nrow(treat.eff))


flat<- apply(preds$Treat - preds$Control, 1, mean)

mses[ncol(treat.eff) + 2]<- sqrt(sum( (flat - true.eff)^2)/nrow(treat.eff))

output<- list(mses, preds$Weight, X, ys, treat)
names(output)<- c('mse', 'weight', 'X', 'y', 'treat')
save(output, file=files)
}



#mse.mat[monte,]<- mses
#weight.mat[monte,]<- preds$Weight




#fins<- cbind(preds$Treat, final.est, flat)
#cor.fins<- cor(fins)
#cor.mat[,,z]<- cor.fins

#print(monte)
#print(mses)


#output<- list(mse.mat, weight.mat)
#names(output)<- c('mse', 'weight')
#save(output, file = files)
#return(output)
#}
